<template>
  <el-dialog title="排班类型设置" :visible="value" width="700px" custom-class="dialog-box" @close="onClose" @open="onOpen" :close-on-click-modal="false" :append-to-body="true">
    <el-form @submit.native.prevent="onSubmit" label-width="100px" ref="form" :model="form" :rules="rules">
      <el-form-item label="名称：" prop="title">
        <el-input v-model.trim="form.title"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="类型：" prop="type">
            <el-radio-group :disabled="true" v-model="form.type">
              <el-radio :label="1">门诊</el-radio>
              <el-radio :label="2">住院</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门：" prop="deptId">
            <deptSearch  v-model="form.deptId" :keyword="(curItem?curItem.deptName:'')"></deptSearch>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="时间段：" prop="infosArray">
        <div v-if="form.infosArray.length>0" style="margin-top:5px;">
          <el-tag v-for="(item,index) in form.infosArray" :key="index" class="vertical-middle" style="margin:0 10px 10px 0;" closable @close="onDelTag(index)">{{item.startTime}}~{{item.endTime}}</el-tag>
        </div>
        <el-button  class="vertical-middle" size="mini" @click="dialogTime=true">+添加</el-button>
      </el-form-item>
      <el-form-item label="描述：" prop="describe">
        <el-input v-model.trim="form.describe"></el-input>
      </el-form-item>
      <el-button v-show="false" :loading="btnLoading" native-type="submit"></el-button>
    </el-form>
    <el-dialog title="排班时间设置" :visible.sync="dialogTime" width="400px" custom-class="dialog-box" :close-on-click-modal="false" :append-to-body="true">
      <el-form @submit.native.prevent="onSubmitTime" label-width="100px" ref="formTime" :model="formTime" :rules="rulesTime">
        <el-form-item label="开始时间：" prop="startTime">
          <el-time-picker value-format="HH:mm" format="HH:mm" arrow-control v-model="formTime.startTime"></el-time-picker>
        </el-form-item>
        <el-form-item label="结束时间：" prop="endTime">
          <el-time-picker value-format="HH:mm" format="HH:mm" arrow-control v-model="formTime.endTime"></el-time-picker>
        </el-form-item>
        <el-form-item label="号源：" prop="sourceNumber">
          <el-input v-model.number="formTime.sourceNumber"></el-input>
        </el-form-item>
        <el-button v-show="false" :loading="btnLoading" native-type="submit"></el-button>
      </el-form>
      <span slot="footer">
        <el-button type="primary" :loading="btnLoading" @click="onSubmitTime">确 定</el-button>
      </span>
    </el-dialog>
    <span slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  props: ["value", "curItem"],
  components: {
    deptSearch: () => import("@/pages/_com/deptSearch.vue")
  },
  data() {
    let isZero = (rule, value, callback) => {
      if (value) {
        callback();
      } else {
        callback(new Error("格式有误"));
      }
    };
    return {
      dialogTime: false,
      form: {
        type: 1,
        title: "",
        hpId: "",
        deptId: "",
        describe: "",
        infosJson: "",
        status: 1,
        //------------
        infosArray: [],
        deptName: ""
      },
      formTime: {
        startTime: "08:00",
        endTime: "18:00",
        sourceNumber: ""
      },
      rulesTime: {
        startTime: [{ required: true, message: "此项必填", trigger: "change" }],
        endTime: [{ required: true, message: "此项必填", trigger: "change" }],
        sourceNumber: [
          { required: true, message: "此项必填", trigger: "blur" },
          { type: "number", message: "格式有误", trigger: "blur" },
          { validator: isZero, trigger: "blur" }
        ]
      },
      rules: {
        title: [{ required: true, message: "此项必填", trigger: "blur" }],
        deptId: [{ required: true, message: "此项必填", trigger: "change" }],
        infosArray: [{ required: true, message: "此项必填", trigger: "change" }]
      },
      deptArray: [],
      btnLoading: false
    };
  },
  mounted() {
    let th = this;
    th.form.hpId = th.adminUser.hpId;
  },
  methods: {
    onDelTag(index) {
      this.form.infosArray.splice(index, 1);
    },
    //提交时间
    onSubmitTime() {
      let th = this;
      th.$refs["formTime"].validate(v => {
        if (v) {
          let j = Object.assign({}, th.formTime);
          th.form.infosArray.push(j);
          th.dialogTime = false;
          th.$refs["form"].validateField("infosArray");
        }
      });
    },
    onSubmit() {
      let th = this;
      let j = {};
      let add = function() {
        th.btnLoading = true;
        th.$api.addScheduleType(j).then(res => {
          if (res.executed) {
            th.$message.success("操作成功");
            th.$emit("success");
            th.onClose();
          } else {
            th.$message.error(res.message);
          }
          th.btnLoading = false;
        });
      };
      let update = function() {
        j.typeId = th.curItem.id;
        th.btnLoading = true;
        th.$api.updateScheduleType(j).then(res => {
          if (res.executed) {
            th.$message.success("操作成功");
            th.$utils.joinFormJson(th.curItem, j);
            th.onClose();
          } else {
            th.$message.error(res.message);
          }
          th.btnLoading = false;
        });
      };
      th.$refs["form"].validate(v => {
        if (v) {
          Object.assign(j, th.form);
          j.infosJson = JSON.stringify(j.infosArray);
          delete j.infosArray;
          let deptinfo = th.deptArray.find(d => {
            return d.deptId == j.deptId;
          });
          if (deptinfo) j.deptName = deptinfo.deptName;
          if (th.curItem) {
            update();
            // th.onClose();
          } else {
            add();
          }
        }
      });
    },
    onOpen() {
      let th = this;
      th.$nextTick(function() {
        if (th.curItem) {
          th.form.infosArray.push(...th.curItem.times);
          th.$utils.joinFormJson(th.form, th.curItem);
        }
      });
    },
    onClose() {
      let th = this;
      th.form.infosArray = [];
      th.$refs["form"].resetFields();
      th.$emit("input", false);
    }
  }
};
</script>
